import { _decorator, Component, EditBox, input, Label, Node, RichText } from 'cc'
const { ccclass, property } = _decorator

@ccclass('SunManager')
export class SunManager extends Component {
  private static _instance: SunManager = null

  @property(Number)
  private sunPoint: number = 0

  @property(Label)
  private sunPointLabel: Label = null //阳光

  @property(RichText)
  private testRichText: RichText = null

  // 输入框
  @property(EditBox)
  private NameLabel: EditBox = null

  // 获取实例
  public static get Instance(): SunManager {
    return this._instance
  }

  // 获取公共访问器
  public get SunPoint(): number {
    return this.sunPoint
  }

  // 获取公共访问器
  public get Name(): EditBox {
    return this.NameLabel
  }
  protected onLoad(): void {
    // 确保全局只有一个实例
    if (SunManager._instance == null) {
      SunManager._instance = this
    } else {
      console.log('SunManager is already exist')
      this.node.destroy()
    }
  }

  protected start(): void {
    this.updateSunPointLabel()
    this.testRichText.enabled = true
  }

  private updateSunPointLabel(): void {
    this.sunPointLabel.string = this.sunPoint.toString()
  }

  public subSun(point: number): void {
    this.sunPoint -= point
    if (this.sunPoint < 0) {
      this.sunPoint = 0
    }
    this.updateSunPointLabel()
  }
}
